{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:32:43.798553Z",
     "start_time": "2021-09-23T12:32:43.786162Z"
    }
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.simplefilter('ignore')\n",
    "\n",
    "import os\n",
    "import re\n",
    "import gc\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "pd.set_option('max_columns', None)\n",
    "pd.set_option('max_rows', 200)\n",
    "pd.set_option('float_format', lambda x: '%.3f' % x)\n",
    "\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.metrics import roc_auc_score\n",
    "\n",
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:32.572927Z",
     "start_time": "2021-09-23T12:29:32.500016Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 39)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>known_outstanding_loan</th>\n",
       "      <th>known_dero</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>app_type</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "      <th>isDefault</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1040418</td>\n",
       "      <td>240418</td>\n",
       "      <td>31818.182</td>\n",
       "      <td>3</td>\n",
       "      <td>11.466</td>\n",
       "      <td>1174.910</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>3 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/10/1</td>\n",
       "      <td>2</td>\n",
       "      <td>193</td>\n",
       "      <td>13</td>\n",
       "      <td>2.430</td>\n",
       "      <td>0</td>\n",
       "      <td>556.364</td>\n",
       "      <td>649.091</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7734.231</td>\n",
       "      <td>91.800</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>9927</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1025197</td>\n",
       "      <td>225197</td>\n",
       "      <td>28000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>16.841</td>\n",
       "      <td>670.690</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2013/6/1</td>\n",
       "      <td>0</td>\n",
       "      <td>491</td>\n",
       "      <td>30</td>\n",
       "      <td>11.005</td>\n",
       "      <td>1</td>\n",
       "      <td>715.000</td>\n",
       "      <td>893.750</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>31329.000</td>\n",
       "      <td>54.800</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>40642</td>\n",
       "      <td>1</td>\n",
       "      <td>7.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>45.000</td>\n",
       "      <td>22.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1009360</td>\n",
       "      <td>209360</td>\n",
       "      <td>17272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>8.900</td>\n",
       "      <td>603.320</td>\n",
       "      <td>A</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2014/1/1</td>\n",
       "      <td>4</td>\n",
       "      <td>459</td>\n",
       "      <td>8</td>\n",
       "      <td>6.409</td>\n",
       "      <td>0</td>\n",
       "      <td>774.545</td>\n",
       "      <td>903.636</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>18514.000</td>\n",
       "      <td>57.692</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>154</td>\n",
       "      <td>1</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>28.000</td>\n",
       "      <td>19.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1039708</td>\n",
       "      <td>239708</td>\n",
       "      <td>20000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>4.788</td>\n",
       "      <td>602.300</td>\n",
       "      <td>A</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>157</td>\n",
       "      <td>8</td>\n",
       "      <td>9.205</td>\n",
       "      <td>0</td>\n",
       "      <td>750.000</td>\n",
       "      <td>875.000</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20707.000</td>\n",
       "      <td>42.600</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1027483</td>\n",
       "      <td>227483</td>\n",
       "      <td>15272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>12.790</td>\n",
       "      <td>470.310</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>38</td>\n",
       "      <td>21</td>\n",
       "      <td>15.578</td>\n",
       "      <td>0</td>\n",
       "      <td>609.091</td>\n",
       "      <td>710.606</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>14016.154</td>\n",
       "      <td>30.462</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  user_id  total_loan  year_of_loan  interest  monthly_payment  \\\n",
       "0  1040418   240418   31818.182             3    11.466         1174.910   \n",
       "1  1025197   225197   28000.000             5    16.841          670.690   \n",
       "2  1009360   209360   17272.727             3     8.900          603.320   \n",
       "3  1039708   239708   20000.000             3     4.788          602.300   \n",
       "4  1027483   227483   15272.727             3    12.790          470.310   \n",
       "\n",
       "  class employer_type         industry  work_year  house_exist  censor_status  \\\n",
       "0     C          政府机构              金融业    3 years            0              1   \n",
       "1     C          政府机构              金融业  10+ years            0              2   \n",
       "2     A          政府机构        公共服务、社会组织  10+ years            1              0   \n",
       "3     A         世界五百强           文化和体育业    6 years            0              1   \n",
       "4     C          政府机构  信息传输、软件和信息技术服务业   < 1 year            2              1   \n",
       "\n",
       "  issue_date  use  post_code  region  debt_loan_ratio  del_in_18month  \\\n",
       "0  2016/10/1    2        193      13            2.430               0   \n",
       "1   2013/6/1    0        491      30           11.005               1   \n",
       "2   2014/1/1    4        459       8            6.409               0   \n",
       "3   2015/7/1    0        157       8            9.205               0   \n",
       "4   2016/7/1    0         38      21           15.578               0   \n",
       "\n",
       "   scoring_low  scoring_high  known_outstanding_loan  known_dero  \\\n",
       "0      556.364       649.091                       3           0   \n",
       "1      715.000       893.750                       3           0   \n",
       "2      774.545       903.636                       5           0   \n",
       "3      750.000       875.000                       3           0   \n",
       "4      609.091       710.606                      15           0   \n",
       "\n",
       "   pub_dero_bankrup  recircle_b  recircle_u  initial_list_status  app_type  \\\n",
       "0             0.000    7734.231      91.800                    0         0   \n",
       "1             0.000   31329.000      54.800                    1         0   \n",
       "2             0.000   18514.000      57.692                    1         0   \n",
       "3             0.000   20707.000      42.600                    0         0   \n",
       "4             0.000   14016.154      30.462                    0         0   \n",
       "\n",
       "  earlies_credit_mon  title  policy_code     f0    f1     f2     f3     f4  \\\n",
       "0              1-Dec      5            1  1.000 0.000  4.000  5.000  4.000   \n",
       "1             Apr-90  40642            1  7.000 0.000  4.000 45.000 22.000   \n",
       "2             Oct-91    154            1  6.000 0.000  6.000 28.000 19.000   \n",
       "3              1-Jun      0            1  5.000 0.000 10.000 15.000  9.000   \n",
       "4              2-May      0            1 10.000 0.000  6.000 15.000  4.000   \n",
       "\n",
       "   early_return  early_return_amount  early_return_amount_3mon  isDefault  \n",
       "0             3                 9927                     0.000          0  \n",
       "1             0                    0                     0.000          0  \n",
       "2             0                    0                     0.000          0  \n",
       "3             0                    0                     0.000          0  \n",
       "4             0                    0                     0.000          0  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data = pd.read_csv('raw_data/train_public.csv')\n",
    "\n",
    "print(train_data.shape)\n",
    "train_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.178932Z",
     "start_time": "2021-09-23T12:29:32.575009Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(750000, 42)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>sub_class</th>\n",
       "      <th>work_type</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>house_loan_status</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>marriage</th>\n",
       "      <th>offsprings</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>f5</th>\n",
       "      <th>is_default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>119262</td>\n",
       "      <td>0</td>\n",
       "      <td>12000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>11.530</td>\n",
       "      <td>264.100</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>职员</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2015-06-01</td>\n",
       "      <td>0</td>\n",
       "      <td>814.000</td>\n",
       "      <td>4</td>\n",
       "      <td>5.070</td>\n",
       "      <td>1.000</td>\n",
       "      <td>670.000</td>\n",
       "      <td>674.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>3855.000</td>\n",
       "      <td>23.100</td>\n",
       "      <td>0</td>\n",
       "      <td>Mar-1984</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>17.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>369815</td>\n",
       "      <td>1</td>\n",
       "      <td>8000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>13.980</td>\n",
       "      <td>273.350</td>\n",
       "      <td>C</td>\n",
       "      <td>C3</td>\n",
       "      <td>其他</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>国际组织</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2010-10-01</td>\n",
       "      <td>2</td>\n",
       "      <td>240.000</td>\n",
       "      <td>21</td>\n",
       "      <td>15.040</td>\n",
       "      <td>0.000</td>\n",
       "      <td>725.000</td>\n",
       "      <td>729.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>118632.000</td>\n",
       "      <td>99.900</td>\n",
       "      <td>1</td>\n",
       "      <td>Jan-1992</td>\n",
       "      <td>94.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>787833</td>\n",
       "      <td>2</td>\n",
       "      <td>20000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>17.990</td>\n",
       "      <td>507.760</td>\n",
       "      <td>D</td>\n",
       "      <td>D2</td>\n",
       "      <td>工人</td>\n",
       "      <td>上市企业</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-08-01</td>\n",
       "      <td>0</td>\n",
       "      <td>164.000</td>\n",
       "      <td>20</td>\n",
       "      <td>17.380</td>\n",
       "      <td>1.000</td>\n",
       "      <td>675.000</td>\n",
       "      <td>679.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>15670.000</td>\n",
       "      <td>72.500</td>\n",
       "      <td>0</td>\n",
       "      <td>Oct-1996</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>3.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>671675</td>\n",
       "      <td>3</td>\n",
       "      <td>10700.000</td>\n",
       "      <td>3</td>\n",
       "      <td>10.160</td>\n",
       "      <td>346.070</td>\n",
       "      <td>B</td>\n",
       "      <td>B1</td>\n",
       "      <td>职员</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>电力、热力生产供应业</td>\n",
       "      <td>2 years</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2013-05-01</td>\n",
       "      <td>4</td>\n",
       "      <td>48.000</td>\n",
       "      <td>10</td>\n",
       "      <td>27.870</td>\n",
       "      <td>0.000</td>\n",
       "      <td>710.000</td>\n",
       "      <td>714.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>18859.000</td>\n",
       "      <td>78.600</td>\n",
       "      <td>0</td>\n",
       "      <td>Jul-2000</td>\n",
       "      <td>41646.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>3.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>11.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>245160</td>\n",
       "      <td>4</td>\n",
       "      <td>8000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>8.240</td>\n",
       "      <td>251.580</td>\n",
       "      <td>B</td>\n",
       "      <td>B1</td>\n",
       "      <td>其他</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>5 years</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-04-01</td>\n",
       "      <td>4</td>\n",
       "      <td>122.000</td>\n",
       "      <td>9</td>\n",
       "      <td>3.470</td>\n",
       "      <td>0.000</td>\n",
       "      <td>660.000</td>\n",
       "      <td>664.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8337.000</td>\n",
       "      <td>67.800</td>\n",
       "      <td>1</td>\n",
       "      <td>Mar-2000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>3.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  user_id  total_loan  year_of_loan  interest  monthly_payment  \\\n",
       "0   119262        0   12000.000             5    11.530          264.100   \n",
       "1   369815        1    8000.000             3    13.980          273.350   \n",
       "2   787833        2   20000.000             5    17.990          507.760   \n",
       "3   671675        3   10700.000             3    10.160          346.070   \n",
       "4   245160        4    8000.000             3     8.240          251.580   \n",
       "\n",
       "  class sub_class work_type employer_type         industry  work_year  \\\n",
       "0     B        B5        职员          普通企业              采矿业        NaN   \n",
       "1     C        C3        其他          普通企业             国际组织  10+ years   \n",
       "2     D        D2        工人          上市企业  信息传输、软件和信息技术服务业  10+ years   \n",
       "3     B        B1        职员          普通企业       电力、热力生产供应业    2 years   \n",
       "4     B        B1        其他          政府机构              金融业    5 years   \n",
       "\n",
       "   house_exist  house_loan_status  censor_status  marriage  offsprings  \\\n",
       "0            0                  0              2         0           0   \n",
       "1            0                  1              2         1           3   \n",
       "2            0                  0              1         0           0   \n",
       "3            2                  0              2         0           0   \n",
       "4            1                  2              0         0           0   \n",
       "\n",
       "   issue_date  use  post_code  region  debt_loan_ratio  del_in_18month  \\\n",
       "0  2015-06-01    0    814.000       4            5.070           1.000   \n",
       "1  2010-10-01    2    240.000      21           15.040           0.000   \n",
       "2  2016-08-01    0    164.000      20           17.380           1.000   \n",
       "3  2013-05-01    4     48.000      10           27.870           0.000   \n",
       "4  2017-04-01    4    122.000       9            3.470           0.000   \n",
       "\n",
       "   scoring_low  scoring_high  pub_dero_bankrup  early_return  \\\n",
       "0      670.000       674.000             1.000             0   \n",
       "1      725.000       729.000             0.000             0   \n",
       "2      675.000       679.000             0.000             0   \n",
       "3      710.000       714.000             0.000             0   \n",
       "4      660.000       664.000             0.000             0   \n",
       "\n",
       "   early_return_amount  early_return_amount_3mon  recircle_b  recircle_u  \\\n",
       "0                    0                     0.000    3855.000      23.100   \n",
       "1                    0                     0.000  118632.000      99.900   \n",
       "2                    0                     0.000   15670.000      72.500   \n",
       "3                    0                     0.000   18859.000      78.600   \n",
       "4                    0                     0.000    8337.000      67.800   \n",
       "\n",
       "   initial_list_status earlies_credit_mon     title  policy_code    f0    f1  \\\n",
       "0                    0           Mar-1984     0.000        1.000 1.000 0.000   \n",
       "1                    1           Jan-1992    94.000        1.000   nan   nan   \n",
       "2                    0           Oct-1996     0.000        1.000 6.000 0.000   \n",
       "3                    0           Jul-2000 41646.000        1.000 3.000 0.000   \n",
       "4                    1           Mar-2000     4.000        1.000 3.000 0.000   \n",
       "\n",
       "      f2     f3    f4    f5  is_default  \n",
       "0  8.000 17.000 8.000 1.000           1  \n",
       "1    nan    nan   nan   nan           0  \n",
       "2 10.000  8.000 3.000 0.000           0  \n",
       "3  4.000 11.000 6.000 0.000           0  \n",
       "4  8.000  6.000 4.000 1.000           0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_internet = pd.read_csv('raw_data/train_internet.csv')\n",
    "\n",
    "print(train_internet.shape)\n",
    "train_internet.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.227008Z",
     "start_time": "2021-09-23T12:29:35.180946Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5000, 38)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>known_outstanding_loan</th>\n",
       "      <th>known_dero</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>app_type</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000575</td>\n",
       "      <td>200575</td>\n",
       "      <td>2890.909</td>\n",
       "      <td>3</td>\n",
       "      <td>10.791</td>\n",
       "      <td>88.010</td>\n",
       "      <td>B</td>\n",
       "      <td>幼教与中小学校</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2017/12/1</td>\n",
       "      <td>0</td>\n",
       "      <td>314</td>\n",
       "      <td>0</td>\n",
       "      <td>23.040</td>\n",
       "      <td>0</td>\n",
       "      <td>745.000</td>\n",
       "      <td>869.167</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8647.692</td>\n",
       "      <td>31.846</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>773</td>\n",
       "      <td>89.192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1028125</td>\n",
       "      <td>228125</td>\n",
       "      <td>7272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>9.990</td>\n",
       "      <td>258.100</td>\n",
       "      <td>B</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/1</td>\n",
       "      <td>5</td>\n",
       "      <td>29</td>\n",
       "      <td>19</td>\n",
       "      <td>27.755</td>\n",
       "      <td>0</td>\n",
       "      <td>681.818</td>\n",
       "      <td>738.636</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>9406.154</td>\n",
       "      <td>18.277</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>8.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>29.000</td>\n",
       "      <td>14.000</td>\n",
       "      <td>1</td>\n",
       "      <td>1894</td>\n",
       "      <td>218.538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1010694</td>\n",
       "      <td>210694</td>\n",
       "      <td>26295.455</td>\n",
       "      <td>3</td>\n",
       "      <td>15.763</td>\n",
       "      <td>764.030</td>\n",
       "      <td>C</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2013/4/1</td>\n",
       "      <td>0</td>\n",
       "      <td>488</td>\n",
       "      <td>24</td>\n",
       "      <td>25.495</td>\n",
       "      <td>1</td>\n",
       "      <td>758.182</td>\n",
       "      <td>947.727</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>26414.769</td>\n",
       "      <td>62.300</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>268</td>\n",
       "      <td>1</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>1</td>\n",
       "      <td>5670</td>\n",
       "      <td>1221.231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1026712</td>\n",
       "      <td>226712</td>\n",
       "      <td>22690.909</td>\n",
       "      <td>5</td>\n",
       "      <td>19.305</td>\n",
       "      <td>524.300</td>\n",
       "      <td>D</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2017/12/1</td>\n",
       "      <td>0</td>\n",
       "      <td>489</td>\n",
       "      <td>30</td>\n",
       "      <td>10.620</td>\n",
       "      <td>0</td>\n",
       "      <td>572.727</td>\n",
       "      <td>620.455</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1198.000</td>\n",
       "      <td>7.700</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>12.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>2</td>\n",
       "      <td>4800</td>\n",
       "      <td>443.077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1002895</td>\n",
       "      <td>202895</td>\n",
       "      <td>14545.455</td>\n",
       "      <td>3</td>\n",
       "      <td>7.139</td>\n",
       "      <td>490.320</td>\n",
       "      <td>A</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>金融业</td>\n",
       "      <td>1 year</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016/6/1</td>\n",
       "      <td>2</td>\n",
       "      <td>418</td>\n",
       "      <td>45</td>\n",
       "      <td>6.611</td>\n",
       "      <td>0</td>\n",
       "      <td>638.182</td>\n",
       "      <td>691.364</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>3920.000</td>\n",
       "      <td>8.831</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>7-May</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7.000</td>\n",
       "      <td>14.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3516</td>\n",
       "      <td>649.108</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  user_id  total_loan  year_of_loan  interest  monthly_payment  \\\n",
       "0  1000575   200575    2890.909             3    10.791           88.010   \n",
       "1  1028125   228125    7272.727             3     9.990          258.100   \n",
       "2  1010694   210694   26295.455             3    15.763          764.030   \n",
       "3  1026712   226712   22690.909             5    19.305          524.300   \n",
       "4  1002895   202895   14545.455             3     7.139          490.320   \n",
       "\n",
       "  class employer_type industry  work_year  house_exist  censor_status  \\\n",
       "0     B       幼教与中小学校   住宿和餐饮业    5 years            0              1   \n",
       "1     B          普通企业   批发和零售业  10+ years            1              1   \n",
       "2     C          普通企业   住宿和餐饮业  10+ years            0              2   \n",
       "3     D          普通企业      采矿业  10+ years            0              2   \n",
       "4     A         世界五百强      金融业     1 year            0              0   \n",
       "\n",
       "  issue_date  use  post_code  region  debt_loan_ratio  del_in_18month  \\\n",
       "0  2017/12/1    0        314       0           23.040               0   \n",
       "1   2015/7/1    5         29      19           27.755               0   \n",
       "2   2013/4/1    0        488      24           25.495               1   \n",
       "3  2017/12/1    0        489      30           10.620               0   \n",
       "4   2016/6/1    2        418      45            6.611               0   \n",
       "\n",
       "   scoring_low  scoring_high  known_outstanding_loan  known_dero  \\\n",
       "0      745.000       869.167                       7           0   \n",
       "1      681.818       738.636                      24           0   \n",
       "2      758.182       947.727                      11           0   \n",
       "3      572.727       620.455                       8           0   \n",
       "4      638.182       691.364                      15           0   \n",
       "\n",
       "   pub_dero_bankrup  recircle_b  recircle_u  initial_list_status  app_type  \\\n",
       "0             0.000    8647.692      31.846                    1         0   \n",
       "1             0.000    9406.154      18.277                    0         0   \n",
       "2             0.000   26414.769      62.300                    1         0   \n",
       "3             0.000    1198.000       7.700                    0         0   \n",
       "4             0.000    3920.000       8.831                    1         0   \n",
       "\n",
       "  earlies_credit_mon  title  policy_code    f0    f1     f2     f3     f4  \\\n",
       "0              3-Mar      0            1 2.000 0.000 15.000  5.000  4.000   \n",
       "1             Dec-99      6            1 8.000 0.000  8.000 29.000 14.000   \n",
       "2             Apr-99    268            1 6.000 0.000  4.000 10.000  6.000   \n",
       "3             Jul-00      0            1 4.000 0.000 12.000 10.000  8.000   \n",
       "4              7-May      5            1 4.000 0.000  7.000 14.000  9.000   \n",
       "\n",
       "   early_return  early_return_amount  early_return_amount_3mon  \n",
       "0             3                  773                    89.192  \n",
       "1             1                 1894                   218.538  \n",
       "2             1                 5670                  1221.231  \n",
       "3             2                 4800                   443.077  \n",
       "4             0                 3516                   649.108  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data = pd.read_csv('raw_data/test_public.csv')\n",
    "\n",
    "print(test_data.shape)\n",
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.233964Z",
     "start_time": "2021-09-23T12:29:35.228404Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8317\n",
       "1    1683\n",
       "Name: isDefault, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data['isDefault'].value_counts(dropna=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.385486Z",
     "start_time": "2021-09-23T12:29:35.235507Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    600327\n",
       "1    149673\n",
       "Name: isDefault, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_internet = train_internet.rename(columns={'is_default': 'isDefault'})\n",
    "train_internet['isDefault'].value_counts(dropna=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据整理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.853295Z",
     "start_time": "2021-09-23T12:29:35.387334Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(760000, 35)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "      <th>isDefault</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1040418</td>\n",
       "      <td>31818.182</td>\n",
       "      <td>3</td>\n",
       "      <td>11.466</td>\n",
       "      <td>1174.910</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>3 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/10/1</td>\n",
       "      <td>2</td>\n",
       "      <td>193.000</td>\n",
       "      <td>13</td>\n",
       "      <td>2.430</td>\n",
       "      <td>0.000</td>\n",
       "      <td>556.364</td>\n",
       "      <td>649.091</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7734.231</td>\n",
       "      <td>91.800</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>5.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>9927</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1025197</td>\n",
       "      <td>28000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>16.841</td>\n",
       "      <td>670.690</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2013/6/1</td>\n",
       "      <td>0</td>\n",
       "      <td>491.000</td>\n",
       "      <td>30</td>\n",
       "      <td>11.005</td>\n",
       "      <td>1.000</td>\n",
       "      <td>715.000</td>\n",
       "      <td>893.750</td>\n",
       "      <td>0.000</td>\n",
       "      <td>31329.000</td>\n",
       "      <td>54.800</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>40642.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>7.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>45.000</td>\n",
       "      <td>22.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1009360</td>\n",
       "      <td>17272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>8.900</td>\n",
       "      <td>603.320</td>\n",
       "      <td>A</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2014/1/1</td>\n",
       "      <td>4</td>\n",
       "      <td>459.000</td>\n",
       "      <td>8</td>\n",
       "      <td>6.409</td>\n",
       "      <td>0.000</td>\n",
       "      <td>774.545</td>\n",
       "      <td>903.636</td>\n",
       "      <td>0.000</td>\n",
       "      <td>18514.000</td>\n",
       "      <td>57.692</td>\n",
       "      <td>1</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>154.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>28.000</td>\n",
       "      <td>19.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1039708</td>\n",
       "      <td>20000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>4.788</td>\n",
       "      <td>602.300</td>\n",
       "      <td>A</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>157.000</td>\n",
       "      <td>8</td>\n",
       "      <td>9.205</td>\n",
       "      <td>0.000</td>\n",
       "      <td>750.000</td>\n",
       "      <td>875.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20707.000</td>\n",
       "      <td>42.600</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1027483</td>\n",
       "      <td>15272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>12.790</td>\n",
       "      <td>470.310</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>38.000</td>\n",
       "      <td>21</td>\n",
       "      <td>15.578</td>\n",
       "      <td>0.000</td>\n",
       "      <td>609.091</td>\n",
       "      <td>710.606</td>\n",
       "      <td>0.000</td>\n",
       "      <td>14016.154</td>\n",
       "      <td>30.462</td>\n",
       "      <td>0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  total_loan  year_of_loan  interest  monthly_payment class  \\\n",
       "0  1040418   31818.182             3    11.466         1174.910     C   \n",
       "1  1025197   28000.000             5    16.841          670.690     C   \n",
       "2  1009360   17272.727             3     8.900          603.320     A   \n",
       "3  1039708   20000.000             3     4.788          602.300     A   \n",
       "4  1027483   15272.727             3    12.790          470.310     C   \n",
       "\n",
       "  employer_type         industry  work_year  house_exist  censor_status  \\\n",
       "0          政府机构              金融业    3 years            0              1   \n",
       "1          政府机构              金融业  10+ years            0              2   \n",
       "2          政府机构        公共服务、社会组织  10+ years            1              0   \n",
       "3         世界五百强           文化和体育业    6 years            0              1   \n",
       "4          政府机构  信息传输、软件和信息技术服务业   < 1 year            2              1   \n",
       "\n",
       "  issue_date  use  post_code  region  debt_loan_ratio  del_in_18month  \\\n",
       "0  2016/10/1    2    193.000      13            2.430           0.000   \n",
       "1   2013/6/1    0    491.000      30           11.005           1.000   \n",
       "2   2014/1/1    4    459.000       8            6.409           0.000   \n",
       "3   2015/7/1    0    157.000       8            9.205           0.000   \n",
       "4   2016/7/1    0     38.000      21           15.578           0.000   \n",
       "\n",
       "   scoring_low  scoring_high  pub_dero_bankrup  recircle_b  recircle_u  \\\n",
       "0      556.364       649.091             0.000    7734.231      91.800   \n",
       "1      715.000       893.750             0.000   31329.000      54.800   \n",
       "2      774.545       903.636             0.000   18514.000      57.692   \n",
       "3      750.000       875.000             0.000   20707.000      42.600   \n",
       "4      609.091       710.606             0.000   14016.154      30.462   \n",
       "\n",
       "   initial_list_status earlies_credit_mon     title  policy_code     f0    f1  \\\n",
       "0                    0              1-Dec     5.000        1.000  1.000 0.000   \n",
       "1                    1             Apr-90 40642.000        1.000  7.000 0.000   \n",
       "2                    1             Oct-91   154.000        1.000  6.000 0.000   \n",
       "3                    0              1-Jun     0.000        1.000  5.000 0.000   \n",
       "4                    0              2-May     0.000        1.000 10.000 0.000   \n",
       "\n",
       "      f2     f3     f4  early_return  early_return_amount  \\\n",
       "0  4.000  5.000  4.000             3                 9927   \n",
       "1  4.000 45.000 22.000             0                    0   \n",
       "2  6.000 28.000 19.000             0                    0   \n",
       "3 10.000 15.000  9.000             0                    0   \n",
       "4  6.000 15.000  4.000             0                    0   \n",
       "\n",
       "   early_return_amount_3mon  isDefault  \n",
       "0                     0.000          0  \n",
       "1                     0.000          0  \n",
       "2                     0.000          0  \n",
       "3                     0.000          0  \n",
       "4                     0.000          0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drop1 = ['sub_class', 'work_type', 'house_loan_status', 'marriage', 'offsprings', 'f5']\n",
    "drop2 = ['known_outstanding_loan', 'known_dero', 'app_type']\n",
    "\n",
    "train_internet.drop(drop1 + ['user_id'], axis=1, inplace=True)\n",
    "train_data.drop(drop2 + ['user_id'], axis=1, inplace=True)\n",
    "\n",
    "train_data = pd.concat([train_data, train_internet]).reset_index(drop=True)\n",
    "print(train_data.shape)\n",
    "train_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:35.881315Z",
     "start_time": "2021-09-23T12:29:35.855996Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5000, 34)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000575</td>\n",
       "      <td>2890.909</td>\n",
       "      <td>3</td>\n",
       "      <td>10.791</td>\n",
       "      <td>88.010</td>\n",
       "      <td>B</td>\n",
       "      <td>幼教与中小学校</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2017/12/1</td>\n",
       "      <td>0</td>\n",
       "      <td>314</td>\n",
       "      <td>0</td>\n",
       "      <td>23.040</td>\n",
       "      <td>0</td>\n",
       "      <td>745.000</td>\n",
       "      <td>869.167</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8647.692</td>\n",
       "      <td>31.846</td>\n",
       "      <td>1</td>\n",
       "      <td>3-Mar</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>773</td>\n",
       "      <td>89.192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1028125</td>\n",
       "      <td>7272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>9.990</td>\n",
       "      <td>258.100</td>\n",
       "      <td>B</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>批发和零售业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/1</td>\n",
       "      <td>5</td>\n",
       "      <td>29</td>\n",
       "      <td>19</td>\n",
       "      <td>27.755</td>\n",
       "      <td>0</td>\n",
       "      <td>681.818</td>\n",
       "      <td>738.636</td>\n",
       "      <td>0.000</td>\n",
       "      <td>9406.154</td>\n",
       "      <td>18.277</td>\n",
       "      <td>0</td>\n",
       "      <td>Dec-99</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>8.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>29.000</td>\n",
       "      <td>14.000</td>\n",
       "      <td>1</td>\n",
       "      <td>1894</td>\n",
       "      <td>218.538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1010694</td>\n",
       "      <td>26295.455</td>\n",
       "      <td>3</td>\n",
       "      <td>15.763</td>\n",
       "      <td>764.030</td>\n",
       "      <td>C</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>住宿和餐饮业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2013/4/1</td>\n",
       "      <td>0</td>\n",
       "      <td>488</td>\n",
       "      <td>24</td>\n",
       "      <td>25.495</td>\n",
       "      <td>1</td>\n",
       "      <td>758.182</td>\n",
       "      <td>947.727</td>\n",
       "      <td>0.000</td>\n",
       "      <td>26414.769</td>\n",
       "      <td>62.300</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-99</td>\n",
       "      <td>268</td>\n",
       "      <td>1</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>1</td>\n",
       "      <td>5670</td>\n",
       "      <td>1221.231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1026712</td>\n",
       "      <td>22690.909</td>\n",
       "      <td>5</td>\n",
       "      <td>19.305</td>\n",
       "      <td>524.300</td>\n",
       "      <td>D</td>\n",
       "      <td>普通企业</td>\n",
       "      <td>采矿业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2017/12/1</td>\n",
       "      <td>0</td>\n",
       "      <td>489</td>\n",
       "      <td>30</td>\n",
       "      <td>10.620</td>\n",
       "      <td>0</td>\n",
       "      <td>572.727</td>\n",
       "      <td>620.455</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1198.000</td>\n",
       "      <td>7.700</td>\n",
       "      <td>0</td>\n",
       "      <td>Jul-00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>12.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>2</td>\n",
       "      <td>4800</td>\n",
       "      <td>443.077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1002895</td>\n",
       "      <td>14545.455</td>\n",
       "      <td>3</td>\n",
       "      <td>7.139</td>\n",
       "      <td>490.320</td>\n",
       "      <td>A</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>金融业</td>\n",
       "      <td>1 year</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016/6/1</td>\n",
       "      <td>2</td>\n",
       "      <td>418</td>\n",
       "      <td>45</td>\n",
       "      <td>6.611</td>\n",
       "      <td>0</td>\n",
       "      <td>638.182</td>\n",
       "      <td>691.364</td>\n",
       "      <td>0.000</td>\n",
       "      <td>3920.000</td>\n",
       "      <td>8.831</td>\n",
       "      <td>1</td>\n",
       "      <td>7-May</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7.000</td>\n",
       "      <td>14.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3516</td>\n",
       "      <td>649.108</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  total_loan  year_of_loan  interest  monthly_payment class  \\\n",
       "0  1000575    2890.909             3    10.791           88.010     B   \n",
       "1  1028125    7272.727             3     9.990          258.100     B   \n",
       "2  1010694   26295.455             3    15.763          764.030     C   \n",
       "3  1026712   22690.909             5    19.305          524.300     D   \n",
       "4  1002895   14545.455             3     7.139          490.320     A   \n",
       "\n",
       "  employer_type industry  work_year  house_exist  censor_status issue_date  \\\n",
       "0       幼教与中小学校   住宿和餐饮业    5 years            0              1  2017/12/1   \n",
       "1          普通企业   批发和零售业  10+ years            1              1   2015/7/1   \n",
       "2          普通企业   住宿和餐饮业  10+ years            0              2   2013/4/1   \n",
       "3          普通企业      采矿业  10+ years            0              2  2017/12/1   \n",
       "4         世界五百强      金融业     1 year            0              0   2016/6/1   \n",
       "\n",
       "   use  post_code  region  debt_loan_ratio  del_in_18month  scoring_low  \\\n",
       "0    0        314       0           23.040               0      745.000   \n",
       "1    5         29      19           27.755               0      681.818   \n",
       "2    0        488      24           25.495               1      758.182   \n",
       "3    0        489      30           10.620               0      572.727   \n",
       "4    2        418      45            6.611               0      638.182   \n",
       "\n",
       "   scoring_high  pub_dero_bankrup  recircle_b  recircle_u  \\\n",
       "0       869.167             0.000    8647.692      31.846   \n",
       "1       738.636             0.000    9406.154      18.277   \n",
       "2       947.727             0.000   26414.769      62.300   \n",
       "3       620.455             0.000    1198.000       7.700   \n",
       "4       691.364             0.000    3920.000       8.831   \n",
       "\n",
       "   initial_list_status earlies_credit_mon  title  policy_code    f0    f1  \\\n",
       "0                    1              3-Mar      0            1 2.000 0.000   \n",
       "1                    0             Dec-99      6            1 8.000 0.000   \n",
       "2                    1             Apr-99    268            1 6.000 0.000   \n",
       "3                    0             Jul-00      0            1 4.000 0.000   \n",
       "4                    1              7-May      5            1 4.000 0.000   \n",
       "\n",
       "      f2     f3     f4  early_return  early_return_amount  \\\n",
       "0 15.000  5.000  4.000             3                  773   \n",
       "1  8.000 29.000 14.000             1                 1894   \n",
       "2  4.000 10.000  6.000             1                 5670   \n",
       "3 12.000 10.000  8.000             2                 4800   \n",
       "4  7.000 14.000  9.000             0                 3516   \n",
       "\n",
       "   early_return_amount_3mon  \n",
       "0                    89.192  \n",
       "1                   218.538  \n",
       "2                  1221.231  \n",
       "3                   443.077  \n",
       "4                   649.108  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.drop(drop2 + ['user_id'], axis=1, inplace=True)\n",
    "\n",
    "print(test_data.shape)\n",
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:36.226535Z",
     "start_time": "2021-09-23T12:29:35.883044Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(765000, 35)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>issue_date</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "      <th>isDefault</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1040418</td>\n",
       "      <td>31818.182</td>\n",
       "      <td>3</td>\n",
       "      <td>11.466</td>\n",
       "      <td>1174.910</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>3 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/10/1</td>\n",
       "      <td>2</td>\n",
       "      <td>193.000</td>\n",
       "      <td>13</td>\n",
       "      <td>2.430</td>\n",
       "      <td>0.000</td>\n",
       "      <td>556.364</td>\n",
       "      <td>649.091</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7734.231</td>\n",
       "      <td>91.800</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Dec</td>\n",
       "      <td>5.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>9927</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1025197</td>\n",
       "      <td>28000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>16.841</td>\n",
       "      <td>670.690</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>金融业</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2013/6/1</td>\n",
       "      <td>0</td>\n",
       "      <td>491.000</td>\n",
       "      <td>30</td>\n",
       "      <td>11.005</td>\n",
       "      <td>1.000</td>\n",
       "      <td>715.000</td>\n",
       "      <td>893.750</td>\n",
       "      <td>0.000</td>\n",
       "      <td>31329.000</td>\n",
       "      <td>54.800</td>\n",
       "      <td>1</td>\n",
       "      <td>Apr-90</td>\n",
       "      <td>40642.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>7.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>45.000</td>\n",
       "      <td>22.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1009360</td>\n",
       "      <td>17272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>8.900</td>\n",
       "      <td>603.320</td>\n",
       "      <td>A</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>公共服务、社会组织</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2014/1/1</td>\n",
       "      <td>4</td>\n",
       "      <td>459.000</td>\n",
       "      <td>8</td>\n",
       "      <td>6.409</td>\n",
       "      <td>0.000</td>\n",
       "      <td>774.545</td>\n",
       "      <td>903.636</td>\n",
       "      <td>0.000</td>\n",
       "      <td>18514.000</td>\n",
       "      <td>57.692</td>\n",
       "      <td>1</td>\n",
       "      <td>Oct-91</td>\n",
       "      <td>154.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>28.000</td>\n",
       "      <td>19.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1039708</td>\n",
       "      <td>20000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>4.788</td>\n",
       "      <td>602.300</td>\n",
       "      <td>A</td>\n",
       "      <td>世界五百强</td>\n",
       "      <td>文化和体育业</td>\n",
       "      <td>6 years</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>157.000</td>\n",
       "      <td>8</td>\n",
       "      <td>9.205</td>\n",
       "      <td>0.000</td>\n",
       "      <td>750.000</td>\n",
       "      <td>875.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20707.000</td>\n",
       "      <td>42.600</td>\n",
       "      <td>0</td>\n",
       "      <td>1-Jun</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1027483</td>\n",
       "      <td>15272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>12.790</td>\n",
       "      <td>470.310</td>\n",
       "      <td>C</td>\n",
       "      <td>政府机构</td>\n",
       "      <td>信息传输、软件和信息技术服务业</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2016/7/1</td>\n",
       "      <td>0</td>\n",
       "      <td>38.000</td>\n",
       "      <td>21</td>\n",
       "      <td>15.578</td>\n",
       "      <td>0.000</td>\n",
       "      <td>609.091</td>\n",
       "      <td>710.606</td>\n",
       "      <td>0.000</td>\n",
       "      <td>14016.154</td>\n",
       "      <td>30.462</td>\n",
       "      <td>0</td>\n",
       "      <td>2-May</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  total_loan  year_of_loan  interest  monthly_payment class  \\\n",
       "0  1040418   31818.182             3    11.466         1174.910     C   \n",
       "1  1025197   28000.000             5    16.841          670.690     C   \n",
       "2  1009360   17272.727             3     8.900          603.320     A   \n",
       "3  1039708   20000.000             3     4.788          602.300     A   \n",
       "4  1027483   15272.727             3    12.790          470.310     C   \n",
       "\n",
       "  employer_type         industry  work_year  house_exist  censor_status  \\\n",
       "0          政府机构              金融业    3 years            0              1   \n",
       "1          政府机构              金融业  10+ years            0              2   \n",
       "2          政府机构        公共服务、社会组织  10+ years            1              0   \n",
       "3         世界五百强           文化和体育业    6 years            0              1   \n",
       "4          政府机构  信息传输、软件和信息技术服务业   < 1 year            2              1   \n",
       "\n",
       "  issue_date  use  post_code  region  debt_loan_ratio  del_in_18month  \\\n",
       "0  2016/10/1    2    193.000      13            2.430           0.000   \n",
       "1   2013/6/1    0    491.000      30           11.005           1.000   \n",
       "2   2014/1/1    4    459.000       8            6.409           0.000   \n",
       "3   2015/7/1    0    157.000       8            9.205           0.000   \n",
       "4   2016/7/1    0     38.000      21           15.578           0.000   \n",
       "\n",
       "   scoring_low  scoring_high  pub_dero_bankrup  recircle_b  recircle_u  \\\n",
       "0      556.364       649.091             0.000    7734.231      91.800   \n",
       "1      715.000       893.750             0.000   31329.000      54.800   \n",
       "2      774.545       903.636             0.000   18514.000      57.692   \n",
       "3      750.000       875.000             0.000   20707.000      42.600   \n",
       "4      609.091       710.606             0.000   14016.154      30.462   \n",
       "\n",
       "   initial_list_status earlies_credit_mon     title  policy_code     f0    f1  \\\n",
       "0                    0              1-Dec     5.000        1.000  1.000 0.000   \n",
       "1                    1             Apr-90 40642.000        1.000  7.000 0.000   \n",
       "2                    1             Oct-91   154.000        1.000  6.000 0.000   \n",
       "3                    0              1-Jun     0.000        1.000  5.000 0.000   \n",
       "4                    0              2-May     0.000        1.000 10.000 0.000   \n",
       "\n",
       "      f2     f3     f4  early_return  early_return_amount  \\\n",
       "0  4.000  5.000  4.000             3                 9927   \n",
       "1  4.000 45.000 22.000             0                    0   \n",
       "2  6.000 28.000 19.000             0                    0   \n",
       "3 10.000 15.000  9.000             0                    0   \n",
       "4  6.000 15.000  4.000             0                    0   \n",
       "\n",
       "   early_return_amount_3mon  isDefault  \n",
       "0                     0.000      0.000  \n",
       "1                     0.000      0.000  \n",
       "2                     0.000      0.000  \n",
       "3                     0.000      0.000  \n",
       "4                     0.000      0.000  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.concat([train_data, test_data])\n",
    "\n",
    "print(data.shape)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:36.808964Z",
     "start_time": "2021-09-23T12:29:36.228500Z"
    }
   },
   "outputs": [],
   "source": [
    "data['issue_date'] = pd.to_datetime(data['issue_date'])\n",
    "data['issue_mon'] = data['issue_date'].dt.year * 100 + data['issue_date'].dt.month\n",
    "data.drop(['issue_date'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:36.889563Z",
     "start_time": "2021-09-23T12:29:36.810887Z"
    }
   },
   "outputs": [],
   "source": [
    "data['class'] = data['class'].map({\n",
    "    'A': 0, 'B': 1, 'C': 2, 'D': 3,\n",
    "    'E': 4, 'F': 5, 'G': 6\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:37.051441Z",
     "start_time": "2021-09-23T12:29:36.891539Z"
    }
   },
   "outputs": [],
   "source": [
    "lbe = LabelEncoder()\n",
    "data['employer_type'] = lbe.fit_transform(data['employer_type'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:37.217671Z",
     "start_time": "2021-09-23T12:29:37.053244Z"
    }
   },
   "outputs": [],
   "source": [
    "lbe = LabelEncoder()\n",
    "data['industry'] = lbe.fit_transform(data['industry'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:37.304225Z",
     "start_time": "2021-09-23T12:29:37.219711Z"
    }
   },
   "outputs": [],
   "source": [
    "data['work_year'] = data['work_year'].map({\n",
    "    '< 1 year': 0, '1 year': 1, '2 years': 2, '3 years': 3, '4 years': 4,\n",
    "    '5 years': 5, '6 years': 6, '7 years': 7, '8 years': 8, '9 years': 9,\n",
    "    '10+ years': 10\n",
    "})\n",
    "\n",
    "data['work_year'].fillna(-1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:41.087195Z",
     "start_time": "2021-09-23T12:29:37.306077Z"
    }
   },
   "outputs": [],
   "source": [
    "def clean_mon(x):\n",
    "    mons = {'jan':1, 'feb':2, 'mar':3, 'apr':4,  'may':5,  'jun':6,\n",
    "            'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12}\n",
    "    year_group = re.search('(\\d+)', x)\n",
    "    if year_group:\n",
    "        year = int(year_group.group(1))\n",
    "        if year < 22:\n",
    "            year += 2000\n",
    "        elif 100 > year > 22:\n",
    "            year += 1900\n",
    "        else:\n",
    "            year = 2022\n",
    "    else:\n",
    "        year = 2022\n",
    "        \n",
    "    month_group = re.search('([a-zA-Z]+)', x)\n",
    "    if month_group:\n",
    "        mon = month_group.group(1).lower()\n",
    "        month = mons[mon]\n",
    "    else:\n",
    "        month = 0\n",
    "        \n",
    "    return year*100 + month\n",
    "\n",
    "data['earlies_credit_mon'] = data['earlies_credit_mon'].apply(lambda x: clean_mon(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:41.110027Z",
     "start_time": "2021-09-23T12:29:41.089060Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_id</th>\n",
       "      <th>total_loan</th>\n",
       "      <th>year_of_loan</th>\n",
       "      <th>interest</th>\n",
       "      <th>monthly_payment</th>\n",
       "      <th>class</th>\n",
       "      <th>employer_type</th>\n",
       "      <th>industry</th>\n",
       "      <th>work_year</th>\n",
       "      <th>house_exist</th>\n",
       "      <th>censor_status</th>\n",
       "      <th>use</th>\n",
       "      <th>post_code</th>\n",
       "      <th>region</th>\n",
       "      <th>debt_loan_ratio</th>\n",
       "      <th>del_in_18month</th>\n",
       "      <th>scoring_low</th>\n",
       "      <th>scoring_high</th>\n",
       "      <th>pub_dero_bankrup</th>\n",
       "      <th>recircle_b</th>\n",
       "      <th>recircle_u</th>\n",
       "      <th>initial_list_status</th>\n",
       "      <th>earlies_credit_mon</th>\n",
       "      <th>title</th>\n",
       "      <th>policy_code</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>f3</th>\n",
       "      <th>f4</th>\n",
       "      <th>early_return</th>\n",
       "      <th>early_return_amount</th>\n",
       "      <th>early_return_amount_3mon</th>\n",
       "      <th>isDefault</th>\n",
       "      <th>issue_mon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1040418</td>\n",
       "      <td>31818.182</td>\n",
       "      <td>3</td>\n",
       "      <td>11.466</td>\n",
       "      <td>1174.910</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "      <td>3.000</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>193.000</td>\n",
       "      <td>13</td>\n",
       "      <td>2.430</td>\n",
       "      <td>0.000</td>\n",
       "      <td>556.364</td>\n",
       "      <td>649.091</td>\n",
       "      <td>0.000</td>\n",
       "      <td>7734.231</td>\n",
       "      <td>91.800</td>\n",
       "      <td>0</td>\n",
       "      <td>200112</td>\n",
       "      <td>5.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>3</td>\n",
       "      <td>9927</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>201610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1025197</td>\n",
       "      <td>28000.000</td>\n",
       "      <td>5</td>\n",
       "      <td>16.841</td>\n",
       "      <td>670.690</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "      <td>10.000</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>491.000</td>\n",
       "      <td>30</td>\n",
       "      <td>11.005</td>\n",
       "      <td>1.000</td>\n",
       "      <td>715.000</td>\n",
       "      <td>893.750</td>\n",
       "      <td>0.000</td>\n",
       "      <td>31329.000</td>\n",
       "      <td>54.800</td>\n",
       "      <td>1</td>\n",
       "      <td>199004</td>\n",
       "      <td>40642.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>7.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>45.000</td>\n",
       "      <td>22.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>201306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1009360</td>\n",
       "      <td>17272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>8.900</td>\n",
       "      <td>603.320</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>10.000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>459.000</td>\n",
       "      <td>8</td>\n",
       "      <td>6.409</td>\n",
       "      <td>0.000</td>\n",
       "      <td>774.545</td>\n",
       "      <td>903.636</td>\n",
       "      <td>0.000</td>\n",
       "      <td>18514.000</td>\n",
       "      <td>57.692</td>\n",
       "      <td>1</td>\n",
       "      <td>199110</td>\n",
       "      <td>154.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>28.000</td>\n",
       "      <td>19.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>201401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1039708</td>\n",
       "      <td>20000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>4.788</td>\n",
       "      <td>602.300</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>6.000</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>157.000</td>\n",
       "      <td>8</td>\n",
       "      <td>9.205</td>\n",
       "      <td>0.000</td>\n",
       "      <td>750.000</td>\n",
       "      <td>875.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20707.000</td>\n",
       "      <td>42.600</td>\n",
       "      <td>0</td>\n",
       "      <td>200106</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>9.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>201507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1027483</td>\n",
       "      <td>15272.727</td>\n",
       "      <td>3</td>\n",
       "      <td>12.790</td>\n",
       "      <td>470.310</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>38.000</td>\n",
       "      <td>21</td>\n",
       "      <td>15.578</td>\n",
       "      <td>0.000</td>\n",
       "      <td>609.091</td>\n",
       "      <td>710.606</td>\n",
       "      <td>0.000</td>\n",
       "      <td>14016.154</td>\n",
       "      <td>30.462</td>\n",
       "      <td>0</td>\n",
       "      <td>200205</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>10.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6.000</td>\n",
       "      <td>15.000</td>\n",
       "      <td>4.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>201607</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   loan_id  total_loan  year_of_loan  interest  monthly_payment  class  \\\n",
       "0  1040418   31818.182             3    11.466         1174.910      2   \n",
       "1  1025197   28000.000             5    16.841          670.690      2   \n",
       "2  1009360   17272.727             3     8.900          603.320      0   \n",
       "3  1039708   20000.000             3     4.788          602.300      0   \n",
       "4  1027483   15272.727             3    12.790          470.310      2   \n",
       "\n",
       "   employer_type  industry  work_year  house_exist  censor_status  use  \\\n",
       "0              3        13      3.000            0              1    2   \n",
       "1              3        13     10.000            0              2    0   \n",
       "2              3         3     10.000            1              0    4   \n",
       "3              1        10      6.000            0              1    0   \n",
       "4              3         2      0.000            2              1    0   \n",
       "\n",
       "   post_code  region  debt_loan_ratio  del_in_18month  scoring_low  \\\n",
       "0    193.000      13            2.430           0.000      556.364   \n",
       "1    491.000      30           11.005           1.000      715.000   \n",
       "2    459.000       8            6.409           0.000      774.545   \n",
       "3    157.000       8            9.205           0.000      750.000   \n",
       "4     38.000      21           15.578           0.000      609.091   \n",
       "\n",
       "   scoring_high  pub_dero_bankrup  recircle_b  recircle_u  \\\n",
       "0       649.091             0.000    7734.231      91.800   \n",
       "1       893.750             0.000   31329.000      54.800   \n",
       "2       903.636             0.000   18514.000      57.692   \n",
       "3       875.000             0.000   20707.000      42.600   \n",
       "4       710.606             0.000   14016.154      30.462   \n",
       "\n",
       "   initial_list_status  earlies_credit_mon     title  policy_code     f0  \\\n",
       "0                    0              200112     5.000        1.000  1.000   \n",
       "1                    1              199004 40642.000        1.000  7.000   \n",
       "2                    1              199110   154.000        1.000  6.000   \n",
       "3                    0              200106     0.000        1.000  5.000   \n",
       "4                    0              200205     0.000        1.000 10.000   \n",
       "\n",
       "     f1     f2     f3     f4  early_return  early_return_amount  \\\n",
       "0 0.000  4.000  5.000  4.000             3                 9927   \n",
       "1 0.000  4.000 45.000 22.000             0                    0   \n",
       "2 0.000  6.000 28.000 19.000             0                    0   \n",
       "3 0.000 10.000 15.000  9.000             0                    0   \n",
       "4 0.000  6.000 15.000  4.000             0                    0   \n",
       "\n",
       "   early_return_amount_3mon  isDefault  issue_mon  \n",
       "0                     0.000      0.000     201610  \n",
       "1                     0.000      0.000     201306  \n",
       "2                     0.000      0.000     201401  \n",
       "3                     0.000      0.000     201507  \n",
       "4                     0.000      0.000     201607  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:29:41.116122Z",
     "start_time": "2021-09-23T12:29:41.111295Z"
    }
   },
   "outputs": [],
   "source": [
    "# TODO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:31:47.854136Z",
     "start_time": "2021-09-23T12:29:41.117495Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Fold_1 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[313]\ttrain's auc: 0.818205\tvalid's auc: 0.806095\n",
      "\n",
      "Fold_2 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[377]\ttrain's auc: 0.821227\tvalid's auc: 0.805393\n",
      "\n",
      "Fold_3 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[395]\ttrain's auc: 0.82171\tvalid's auc: 0.805808\n",
      "\n",
      "Fold_4 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[438]\ttrain's auc: 0.8229\tvalid's auc: 0.807024\n",
      "\n",
      "Fold_5 Training ================================\n",
      "\n",
      "[LightGBM] [Warning] feature_fraction is set=0.6, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.6\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[392]\ttrain's auc: 0.821207\tvalid's auc: 0.806122\n"
     ]
    }
   ],
   "source": [
    "train = data[data['isDefault'].notna()]\n",
    "test  = data[data['isDefault'].isna()]\n",
    "\n",
    "ycol = 'isDefault'\n",
    "feature_names = list(\n",
    "    filter(lambda x: x not in [ycol, 'loan_id'], train.columns))\n",
    "\n",
    "model = lgb.LGBMClassifier(objective='binary',\n",
    "                           boosting_type='gbdt',\n",
    "                           tree_learner='serial',\n",
    "                           num_leaves=32,\n",
    "                           max_depth=6,\n",
    "                           learning_rate=0.1,\n",
    "                           n_estimators=10000,\n",
    "                           subsample=0.8,\n",
    "                           feature_fraction=0.6,\n",
    "                           reg_alpha=0.5,\n",
    "                           reg_lambda=0.5,\n",
    "                           random_state=2021,\n",
    "                           is_unbalance=True,\n",
    "                           metric='auc')\n",
    "\n",
    "\n",
    "oof = []\n",
    "prediction = test[['loan_id']]\n",
    "prediction[ycol] = 0\n",
    "df_importance_list = []\n",
    "\n",
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2021)\n",
    "for fold_id, (trn_idx, val_idx) in enumerate(kfold.split(train[feature_names], train[ycol])):\n",
    "    X_train = train.iloc[trn_idx][feature_names]\n",
    "    Y_train = train.iloc[trn_idx][ycol]\n",
    "\n",
    "    X_val = train.iloc[val_idx][feature_names]\n",
    "    Y_val = train.iloc[val_idx][ycol]\n",
    "\n",
    "    print('\\nFold_{} Training ================================\\n'.format(fold_id+1))\n",
    "\n",
    "    lgb_model = model.fit(X_train,\n",
    "                          Y_train,\n",
    "                          eval_names=['train', 'valid'],\n",
    "                          eval_set=[(X_train, Y_train), (X_val, Y_val)],\n",
    "                          verbose=500,\n",
    "                          eval_metric='auc',\n",
    "                          early_stopping_rounds=50)\n",
    "\n",
    "    pred_val = lgb_model.predict_proba(\n",
    "        X_val, num_iteration=lgb_model.best_iteration_)\n",
    "    df_oof = train.iloc[val_idx][['loan_id', ycol]].copy()\n",
    "    df_oof['pred'] = pred_val[:, 1]\n",
    "    oof.append(df_oof)\n",
    "\n",
    "    pred_test = lgb_model.predict_proba(\n",
    "        test[feature_names], num_iteration=lgb_model.best_iteration_)\n",
    "    prediction[ycol] += pred_test[:, 1] / kfold.n_splits\n",
    "\n",
    "    df_importance = pd.DataFrame({\n",
    "        'column': feature_names,\n",
    "        'importance': lgb_model.feature_importances_,\n",
    "    })\n",
    "    df_importance_list.append(df_importance)\n",
    "\n",
    "    del lgb_model, pred_val, pred_test, X_train, Y_train, X_val, Y_val\n",
    "    gc.collect()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:31:47.877232Z",
     "start_time": "2021-09-23T12:31:47.856365Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>issue_mon</td>\n",
       "      <td>1015.600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>recircle_b</td>\n",
       "      <td>723.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>interest</td>\n",
       "      <td>685.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>debt_loan_ratio</td>\n",
       "      <td>649.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>region</td>\n",
       "      <td>641.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>recircle_u</td>\n",
       "      <td>634.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>post_code</td>\n",
       "      <td>622.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>monthly_payment</td>\n",
       "      <td>608.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>total_loan</td>\n",
       "      <td>527.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>f2</td>\n",
       "      <td>501.600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>f3</td>\n",
       "      <td>457.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>employer_type</td>\n",
       "      <td>428.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>scoring_low</td>\n",
       "      <td>411.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>title</td>\n",
       "      <td>396.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>f0</td>\n",
       "      <td>392.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>f4</td>\n",
       "      <td>392.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>earlies_credit_mon</td>\n",
       "      <td>363.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>scoring_high</td>\n",
       "      <td>307.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>work_year</td>\n",
       "      <td>290.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>use</td>\n",
       "      <td>261.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>house_exist</td>\n",
       "      <td>217.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>industry</td>\n",
       "      <td>198.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>class</td>\n",
       "      <td>185.600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>year_of_loan</td>\n",
       "      <td>142.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>del_in_18month</td>\n",
       "      <td>132.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>censor_status</td>\n",
       "      <td>116.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>early_return_amount</td>\n",
       "      <td>103.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>pub_dero_bankrup</td>\n",
       "      <td>98.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>f1</td>\n",
       "      <td>79.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>early_return</td>\n",
       "      <td>69.600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>early_return_amount_3mon</td>\n",
       "      <td>55.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>initial_list_status</td>\n",
       "      <td>19.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>policy_code</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      column  importance\n",
       "0                  issue_mon    1015.600\n",
       "1                 recircle_b     723.800\n",
       "2                   interest     685.200\n",
       "3            debt_loan_ratio     649.400\n",
       "4                     region     641.000\n",
       "5                 recircle_u     634.000\n",
       "6                  post_code     622.200\n",
       "7            monthly_payment     608.800\n",
       "8                 total_loan     527.200\n",
       "9                         f2     501.600\n",
       "10                        f3     457.800\n",
       "11             employer_type     428.200\n",
       "12               scoring_low     411.000\n",
       "13                     title     396.400\n",
       "14                        f0     392.800\n",
       "15                        f4     392.400\n",
       "16        earlies_credit_mon     363.200\n",
       "17              scoring_high     307.800\n",
       "18                 work_year     290.200\n",
       "19                       use     261.800\n",
       "20               house_exist     217.800\n",
       "21                  industry     198.200\n",
       "22                     class     185.600\n",
       "23              year_of_loan     142.400\n",
       "24            del_in_18month     132.200\n",
       "25             censor_status     116.400\n",
       "26       early_return_amount     103.400\n",
       "27          pub_dero_bankrup      98.200\n",
       "28                        f1      79.200\n",
       "29              early_return      69.600\n",
       "30  early_return_amount_3mon      55.800\n",
       "31       initial_list_status      19.200\n",
       "32               policy_code       0.000"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_importance = pd.concat(df_importance_list)\n",
    "df_importance = df_importance.groupby(['column'])['importance'].agg(\n",
    "    'mean').sort_values(ascending=False).reset_index()\n",
    "df_importance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:36:15.307395Z",
     "start_time": "2021-09-23T12:36:14.989909Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "roc_auc_score: 0.8060829091394448\n"
     ]
    }
   ],
   "source": [
    "oof = pd.concat(oof)\n",
    "print('roc_auc_score:', roc_auc_score(oof['isDefault'], oof['pred']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 提交"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:37:55.912030Z",
     "start_time": "2021-09-23T12:37:55.897023Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>isDefault</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000575</td>\n",
       "      <td>0.077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1028125</td>\n",
       "      <td>0.147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1010694</td>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1026712</td>\n",
       "      <td>0.020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1002895</td>\n",
       "      <td>0.019</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id  isDefault\n",
       "0  1000575      0.077\n",
       "1  1028125      0.147\n",
       "2  1010694      0.007\n",
       "3  1026712      0.020\n",
       "4  1002895      0.019"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction.columns = ['id', 'isDefault']\n",
    "prediction.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:36:52.431527Z",
     "start_time": "2021-09-23T12:36:52.415326Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count   5000.000\n",
       "mean       0.273\n",
       "std        0.307\n",
       "min        0.000\n",
       "25%        0.030\n",
       "50%        0.087\n",
       "75%        0.572\n",
       "max        0.954\n",
       "Name: isDefault, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction['isDefault'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-23T12:38:04.933455Z",
     "start_time": "2021-09-23T12:38:04.901708Z"
    }
   },
   "outputs": [],
   "source": [
    "prediction.to_csv('baseline.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
